#version 450 core

layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;

layout(std140, binding = 0) uniform PerDraw
{
    mat4 viewMatrix;
    mat4 projMatrix;
    float time;
};

in Varyings
{
    vec2 uv;
} i[];

out Varyings
{
    vec2 uv;
} o;

vec3 GetNormal(vec4 v0, vec4 v1, vec4 v2)
{
    vec3 a = normalize(vec3(v0 - v1));
    vec3 b = normalize(vec3(v2 - v1));
    return cross(a, b);
}

vec4 Explode(vec4 position, vec3 normal)
{
    const float magnitude = 2.0;
    return position + vec4(normal * (sin(time) * 0.5 + 0.5) * magnitude, 0.0);
}

// 爆破效果
void main()
{
    vec3 normal = GetNormal(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_in[2].gl_Position);

    gl_Position = Explode(gl_in[0].gl_Position, normal);
    o.uv = i[0].uv;
    EmitVertex();
    gl_Position = Explode(gl_in[1].gl_Position, normal);
    o.uv = i[1].uv;
    EmitVertex();
    gl_Position = Explode(gl_in[2].gl_Position, normal);
    o.uv = i[2].uv;
    EmitVertex();

    EndPrimitive();
}
